//
//  NewfeatureViewController.swift
//  weibo
//
//  Created by Fosen on 15/11/30.
//  Copyright © 2015年 fosen. All rights reserved.
//

import UIKit
import SnapKit
private let reuseIdentifier = "Cell"
private let maxImageCount = 4
class NewfeatureViewController: UICollectionViewController {

    init() {
        
        let collectionViewLayout = UICollectionViewFlowLayout()
        
        collectionViewLayout.itemSize = UIScreen.mainScreen().bounds.size
        collectionViewLayout.scrollDirection = .Horizontal
        collectionViewLayout.minimumInteritemSpacing = 0
        collectionViewLayout.minimumLineSpacing = 0
        super.init(collectionViewLayout: collectionViewLayout)
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(NewFeatureCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return maxImageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCollectionViewCell
    
        // Configure the cell
        cell.index = indexPath.item
        return cell
    }

    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //获取collectionView 的当前显示的cell
        let cell = collectionView.visibleCells().last as! NewFeatureCollectionViewCell
        let indexItem = collectionView.indexPathForCell(cell)?.item ?? 0
        if indexItem == maxImageCount - 1{
            cell.startAnimation()
        
        }
//        if indexPath.item == 2{
//        
//        cell.startAnimation()
//        
//        }
        
        print(indexPath.item)
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

//自定义cell
class NewFeatureCollectionViewCell :UICollectionViewCell{

    
    var index: Int = 0{
    
        didSet{
        iconView.image = UIImage(named: "new_feature_\(index + 1)")
        startBtn.hidden = true
        }
    
    }
    //重写初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
     
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        contentView.addSubview(iconView)
        contentView.addSubview(startBtn)
    
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView.snp_edges)
        }
        
        startBtn.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView.snp_bottom).offset(-100)
        }
        
        startBtn.addTarget(self, action: "startBtnDidClick", forControlEvents: .TouchUpInside)
        
    }
    
    func startAnimation(){
    
        //动画开始之前 按钮的显示状态
        startBtn.hidden = false
        
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.682, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
//            CGAffineTransformIdentity属性能还原到 进行动画 之前的状态
            self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("OK")
        }
    
    }
    
    
    @objc private func startBtnDidClick(){
    
        print(__FUNCTION__)
        NSNotificationCenter.defaultCenter().postNotificationName(WebSwitchRootVCNotification, object: nil)
    
    }
    
    //懒加载子视图
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    private lazy var startBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("开始体验", forState: .Normal)

        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        btn.sizeToFit()
        return btn
        
    }()


}